package com.hantang.web.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;

import com.hantang.web.dao.ds.NewMessagePO;
import com.hantang.web.data.NewMessageData;

public class NewMessageDao extends BaseDao {

	private static Logger logger = Logger.getLogger(NewMessageDao.class);
	
	public static NewMessagePO convertData2Po(NewMessageData data)
	{
		if(data == null){
			return null;
		}
		NewMessagePO po = new NewMessagePO();
		po.setContent(data.getContent());
		if(data.getCreateDate() == null){
			data.setCreateDate(new Date());
		}
		po.setCreateDate(data.getCreateDate());
		po.setHighlight(data.getHighlight());
		po.setId(data.getId());
		po.setRemarks(data.getRemarks());
		po.setSequence(data.getSequence());
		po.setTitle(data.getTitle());
		return po;
	}
	
	public static NewMessageData convertPo2Data(NewMessagePO p)
	{
		if(p == null){
			return null;
		}
		NewMessageData d = new NewMessageData();
		d.setContent(p.getContent());
		d.setCreateDate(p.getCreateDate());
		d.setHighlight(p.getHighlight());
		d.setId(p.getId());
		d.setRemarks(p.getRemarks());
		d.setSequence(p.getSequence());
		d.setTitle(p.getTitle());
		return d;
	}
	
	public boolean deleteData(long id)
	{
		logger.info("deleteData, id:"  + id);
		NewMessagePO po = new NewMessagePO();
		po.setId(id);
		boolean isOk = this.delete(po);
		return isOk;
	}
	
	public long saveData(NewMessageData data)
	{
		if(data == null){
			logger.error("saveData, data is null!");
			return -1;
		}
		NewMessagePO po = convertData2Po(data);
		this.save(po);
		data.setId(po.getId());
		logger.info("saveData, return Id:" + po.getId());
		return po.getId();
	}
	
	public boolean editData(NewMessageData data)
	{
		boolean isOk = false;
		
		Transaction tx = null;
		 try{
			 this.openSession();	
			 NewMessagePO obj = (NewMessagePO)this.session.get(NewMessagePO.class, data.getId());
			 if(obj != null){
				 NewMessagePO po = convertData2Po(data);
				 tx = this.session.beginTransaction();
				 this.session.update(po);
				 tx.commit();
				 isOk = true;
			 }
		 }catch(HibernateException e){
			 logger.error(e.getStackTrace());
			 e.printStackTrace();
			 if(tx != null){
				 tx.rollback();
			 }
		 }
		 finally{
			 this.closeSession();				 
		 }
		return isOk;
		
	}
	
	public NewMessageData loadData(long id)
	{
		NewMessageData data = null;
		logger.info("loadData, id:" + id);
		try{
			this.openSession();
			NewMessagePO obj = (NewMessagePO)this.session.get(NewMessagePO.class, id);
			if(obj != null)
			{
				data = convertPo2Data(obj);
			}		
		}catch(Exception e){
			logger.error(e.getStackTrace());
			e.printStackTrace();
		}finally{
			this.closeSession();			
		}
		return data;
	}
	
	@SuppressWarnings("unchecked")
	public List<NewMessageData> queryDatas(long lastId, int count)
	{
		List<NewMessageData> datas = new ArrayList<NewMessageData>();
		try{
			this.openSession();
			Query queryVmIds = session.createQuery("select nm from NewMessagePO as nm where nm.id < ? and nm.highlight != -1 order by nm.id DESC")
								.setLong(0, lastId).setMaxResults(count);//.setInteger(1, count);
			
			List<NewMessagePO> results = queryVmIds.list();
			if (!results.isEmpty()) {
				for (int i = 0; i < results.size(); i++)
				{
					NewMessageData d = convertPo2Data(results.get(i));
					datas.add(d);
				}
			}		
		}catch(Exception e){
			logger.error(e.getStackTrace());
			e.printStackTrace();
			datas = null;
		}finally{
			this.closeSession();			
		}
		logger.info("queryDatas, fromId " + lastId + " count "+ count + ", return size:" + datas.size());
		
		return datas; // do not return a "null", since yaml doesn't like a null input.
	}
	
}
